<template>
    <!-- 添加或修改产品碳足迹对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品分类" prop="level0">
          <el-cascader style="width: 100%"
                       v-model="categorizeArr" @change="changeProductCategorize" :show-all-levels="false"
                       :options="$store.state.factorDict.productCategorizeDict0"
                       :props="{value:'code',label:'chineseName' }"
                       :clearable="false"></el-cascader>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-select v-model="form.productId" placeholder="请选择产品"     :clearable="false" style="width: 100%" @change="changeProduct">
            <el-option v-for="dict in productInfoOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属企业" prop="enterpriseId">
          <el-cascader  v-model="enterpriseIdArr" style="width: 100%"
                        :options="$store.state.factorDict.enterpriseDict" :show-all-levels="false"
                        :props="{value:'id',label:'enterpriseName',children:'children',checkStrictly:true,}"
                        :clearable="false"></el-cascader>
        </el-form-item>
<!--        <el-form-item label="产品名称" prop="productName">-->
<!--          <el-input v-model="form.productName" placeholder="请输入产品名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="产品批次" prop="productBatch">
          <el-input v-model="form.productBatch" placeholder="请输入产品批次" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { listProductCarbon, getProductCarbon, delProductCarbon, addProductCarbon, updateProductCarbon, exportProductCarbon } from "@/api/carbon/productCarbon";
import { listProductInfo } from "@/api/factor/productInfo";
export default {
  name: "ProductCarbonForm",
  data() {
    return {
      categorizeArr:[],
      enterpriseIdArr:[],
      productInfoOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        level0: [{ required: true, message: "请选择产品分类", trigger: "change" }],
        productName: [{ required: true, message: "请选择产品", trigger: "change" }],
        enterpriseId: [{ required: true, message: "请选择企业", trigger: "change" }],
        productBatch: [{ required: true, message: "请输入产品批次", trigger: "blur" }],
      },
    };
  },
  created() {
  },
  methods: {
    changeProductCategorize(){
      let params={};
      if(this.categorizeArr.length>0){
        params.level0 = this.categorizeArr[0]
      }
      if(this.categorizeArr.length>1){
        params.level1 = this.categorizeArr[1]
      }
      if(this.categorizeArr.length>2){
        params.level2 = this.categorizeArr[2]
      }
      if(this.categorizeArr.length>3){
        params.level3 = this.categorizeArr[3]
      }
      listProductInfo(params).then(response => {
        this.productInfoOptions = response.rows;
      });
    },
    changeProduct(){
      for (let item of this.productInfoOptions){
        if(item.id===this.form.productId){
          this.form.productId = item['id'];
          this.form.productName = item['name'];
          return
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        level0: null,
        level1: null,
        productId: null,
        level2: null,
        level3: null,
        productName: null,
        enterpriseId: null,
        productBatch: null,
        carbonAccountQuantity: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.categorizeArr=[];
      this.enterpriseIdArr=[];
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品碳足迹";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getProductCarbon(row.id).then(response => {
        let categorizeArr = [];
        if(response.data.level0){
          categorizeArr.push(response.data.level0)
        }
        if(response.data.level1){
          categorizeArr.push(response.data.level1)
        }
        if(response.data.level2){
          categorizeArr.push(response.data.level2)
        }
        if(response.data.level3){
          categorizeArr.push(response.data.level3)
        }
        this.categorizeArr=categorizeArr;
        this.changeProductCategorize();
        if(response.data&&response.data.enterpriseId){
          let list = this.$store.state.factorDict.enterpriseDict;
          this.getEnterpriseByChildren(list, response.data.enterpriseId);
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改产品碳足迹";
      });
    },
    getEnterpriseByChildren(list, menuId, parentIds) {
      for (let i = 0; i < list.length; i++) {
        if(list[i]){
          let arr = [];
          if(parentIds&&parentIds.length>0){
            for (let parentId of parentIds)
            arr.push(parentId);
          }
            arr.push(list[i].id);
          if (list[i].id === Number(menuId)) {
            this.enterpriseIdArr =arr;
            return ;
          } else {
            if (list[i].children && list[i].children.length > 0) {
              this.getEnterpriseByChildren(list[i].children, menuId, arr)
            }
          }
        }
      }
    },


    /** 提交按钮 */
    submitForm() {
      if(this.categorizeArr.length>0){
        this.form.level0 = this.categorizeArr[0]
      }
      if(this.categorizeArr.length>1){
        this.form.level1 = this.categorizeArr[1]
      }
      if(this.categorizeArr.length>2){
        this.form.level2 = this.categorizeArr[2]
      }
      if(this.categorizeArr.length>3){
        this.form.level3 = this.categorizeArr[3]
      }
      if(this.enterpriseIdArr.length>0){
        this.form.enterpriseId = this.enterpriseIdArr[this.enterpriseIdArr.length-1];
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductCarbon(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.$emit("getList");
              }
            });
          } else {
            addProductCarbon(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.$emit("getList");
              }
            });
          }
        }
      });
    },

  }
};
</script>
